package com.lishe.lishe.lishe.module.excitation.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lishe.lishe.Imageloader.ImageLoaderUtils;
import com.lishe.lishe.R;
import com.lishe.lishe.lishe.base.BaseActivity;
import com.lishe.lishe.lishe.common.Globals;
import com.lishe.lishe.lishe.module.excitation.adapter.StringAdapter;
import com.lishe.lishe.lishe.module.excitation.bean.user.UserObject;
import com.lishe.lishe.lishe.widget.CircleImageView;
import com.lishe.lishe.lishe.widget.MListDialog;
import com.lishe.lishe.retrofit.DataResponseListener;
import com.lishe.lishe.lishe.module.excitation.presenter.ExcitationPresenter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yang on 2017/11/20.
 * 个人信息维护
 */

public class PersonMessageModifyActivity extends BaseActivity implements View.OnClickListener {

    public static final int REQUEST_CODE_MODIFY_HEAD = 1001;
    public static final int REQUEST_CODE_MODIFY_NAME = 1002;
    public static final int REQUEST_CODE_MODIFY_GENDER = 1003;
    public static final int REQUEST_CODE_MODIFY_EMAIL = 1004;

    public static void jump(Activity activity, UserObject userObject) {
        Intent intent = new Intent(activity, PersonMessageModifyActivity.class);
        mUserObject = userObject;
        activity.startActivity(intent);
    }

    private LinearLayout mLinearHead,
            mLinearName,
            mLinearSex,
            mLinearPhone,
            mLinearEmail,
            mLinearEnterprise,
            mLinearDepartment,
            mLinearJob;
    private TextView mTextName,
            mTextSex,
            mTextPhone,
            mTextEmail,
            mTextEnterprise,
            mTextDepartment,
            mTextJob, mTextTitle;
    private CircleImageView mImageHead;
    private Button mButtonBack;
    private Button moreButton;
    private static UserObject mUserObject = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_message_modify);
        initView();
        setListener();
        if (mUserObject == null) {
            getUserInfo();
        } else {
            updateView(mUserObject);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            updateView(mUserObject);
        }
    }

    private void getUserInfo() {
        try {
            ExcitationPresenter.getInstance().getUserInfo(Globals.getToken(), new DataResponseListener<UserObject>() {
                @Override
                public void onSuccess(UserObject dataResponse) {
                    mUserObject = dataResponse;
                    updateView(dataResponse);
                }

                @Override
                public void onFail(String failMessage) {
                    toast(failMessage);
                }

                @Override
                public void onError(Throwable e) {
                    e.printStackTrace();
                    toast(getString(R.string.interfacee_access_error));
                }

            });
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            toast(getString(R.string.data_parse_fail));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void updateView(UserObject dataResponse) {
        if (!TextUtils.isEmpty(dataResponse.getUserAvatarPath())) {
            ImageLoaderUtils.displayImage(this, dataResponse.getUserAvatarPath(), mImageHead,R.mipmap.head);
        }

        mTextName.setText(dataResponse.getEmpName());
        if (!TextUtils.isEmpty(dataResponse.getGender())) {
            mTextSex.setText(dataResponse.getGender().equals("M") ? "男" : "女");
        }
        mTextPhone.setText(dataResponse.getPhoneNo());
        mTextEmail.setText(TextUtils.isEmpty(dataResponse.getEmpEmail()) ? "无" : dataResponse.getEmpEmail());
        mTextEnterprise.setText(dataResponse.getComName());
        mTextDepartment.setText(TextUtils.isEmpty(dataResponse.getDeptName()) ? "无" : dataResponse.getDeptName());
        mTextJob.setText(TextUtils.isEmpty(dataResponse.getPosName()) ? "无" : dataResponse.getPosName());
    }

    private void initView() {
        mLinearHead = findViewById(R.id.ll_head);
        mLinearName = findViewById(R.id.ll_name);
        mLinearSex = findViewById(R.id.ll_sex);
        mLinearPhone = findViewById(R.id.ll_phone);
        mLinearEmail = findViewById(R.id.ll_email);
        mLinearEnterprise = findViewById(R.id.ll_enterprise);
        mLinearDepartment = findViewById(R.id.ll_department);
        mLinearJob = findViewById(R.id.ll_job);

        mTextName = findViewById(R.id.name);
        mTextSex = findViewById(R.id.sex);
        mTextPhone = findViewById(R.id.phone);
        mTextEmail = findViewById(R.id.email);
        mTextEnterprise = findViewById(R.id.enterprise);
        mTextDepartment = findViewById(R.id.department);
        mTextJob = findViewById(R.id.job);
        mTextTitle = findViewById(R.id.title_text_title);
        mButtonBack = findViewById(R.id.title_left_button);
        mImageHead = findViewById(R.id.head);
        moreButton = findViewById(R.id.more_button);
        moreButton.setVisibility(View.VISIBLE);
        mTextTitle.setText("个人信息");
    }

    private void setListener() {
        mButtonBack.setOnClickListener(this);

        mLinearHead.setOnClickListener(this);
        mLinearName.setOnClickListener(this);
        mLinearSex.setOnClickListener(this);
        mLinearPhone.setOnClickListener(this);
        mLinearEmail.setOnClickListener(this);
        mLinearEnterprise.setOnClickListener(this);
        mLinearDepartment.setOnClickListener(this);
        mLinearJob.setOnClickListener(this);
        moreButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_left_button:
                finish();
                break;
            case R.id.ll_head:
                if (mUserObject != null) {
                    PersonModifyHeadActivity.jump(PersonMessageModifyActivity.this, mUserObject);
                }
                break;
            case R.id.ll_name:
                if (mUserObject != null) {
                    PersonModifyNameActivity.jump(PersonMessageModifyActivity.this, mUserObject, false);
                }
                break;
            case R.id.ll_sex:
                if (mUserObject != null) {
                    PersonModifyGenderActivity.jump(PersonMessageModifyActivity.this, mUserObject);
                }
                break;
            case R.id.ll_phone:
                break;
            case R.id.ll_email:
                if (mUserObject != null) {
                    PersonModifyNameActivity.jump(PersonMessageModifyActivity.this, mUserObject, true);
                }
                break;
            case R.id.ll_enterprise:
                PersonMessageComNameActivity.jump(PersonMessageModifyActivity.this, mUserObject);
                break;
            case R.id.ll_department:
                break;
            case R.id.ll_job:
                break;
            case R.id.more_button:
                createDialog();
                break;
        }
    }

    List<String> list = new ArrayList<>();

    private void createDialog() {
        Log.e("dialog", "出来");
        list.clear();
        list.add("切换企业");
        final MListDialog.Builder dialog = new MListDialog.Builder(this);
        StringAdapter adapter = new StringAdapter(list);
        dialog.setAdapter(adapter);
        dialog.create().show();
        dialog.getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                dialog.dissmiss();
                CompanyListActivity.jump(PersonMessageModifyActivity.this, 1);
            }
        });
    }
}
